<?php
/*
 * File Name: Database.php
 * Date: November 18, 2008
 * Author: Angelo Rodrigues
 * Description: Contains database connection, result
 *              Management functions, input validation
 *
 *              All functions return true if completed
 *              successfully and false if an error
 *              occurred
 *
 */
class Database
{

	/*
	 * Edit the following variables
	 */
// 	private $db_host = '203.162.31.106:3306';     // Database Host
// 	private $db_user = 'vanchuong8x';          // Username
// 	private $db_pass = 'chuong123';          // Password
// 	private $db_name = 'dvgtgt_tourguide';          // Database
	/*	private $db_host = 'localhost';     // Database Host
		private $db_user = 'usaperfe_vu';          // Username
		private $db_pass = 'f9K1=T8L3}Qk';          // Password
		private $db_name = 'usaperfe_vu';          // Databa
		*/
		private $db_host = 'localhost';     // Database Host
		private $db_user = 'root';          // Username
		private $db_pass = '';          // Password
		private $db_name = 'webservice_iphone';          // Databa
	/*
	* End edit
	*/

	private $con = false;               // Checks to see if the connection is active
	private $result = array();          // Results that are returned from the query

	/*
	 * Connects to the database, only one connection
	 * allowed
	 */
	public function connect()
	{
		if(!$this->con)
		{
			$myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
			mysql_set_charset('utf8',$myconn); 
			if($myconn)
			{
				$seldb = @mysql_select_db($this->db_name,$myconn);
				if($seldb)
				{
					$this->con = true;
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
	}

	public function disconnect()
	{
		if($this->con)
		{
			if(@mysql_close())
			{
				$this->con = false;
				return true;
			}
			else
			{
				return false;
			}
		}
	}

	/*
	 * Changes the new database, sets all current results
	 * to null
	 */
	public function setDatabase($name)
	{
		if($this->con)
		{
			if(@mysql_close())
			{
				$this->con = false;
				$this->results = null;
				$this->db_name = $name;
				$this->connect();
			}
		}

	}

	/*
	 * Selects information from the database.
	 * Required: table (the name of the table)
	 * Optional: rows (the columns requested, separated by commas)
	 *           where (column = value as a string)
	 *           order (column DIRECTION as a string)
	 */ 
	
	public function select($query)
	{
		$query = @mysql_query($query);
		if($query)
		{
			$this->numResults = mysql_num_rows($query);
			for($i = 0; $i < $this->numResults; $i++)
			{
				$r = mysql_fetch_array($query);

				$key = array_keys($r);
				for($x = 0; $x < count($key); $x++)
				{
					// Sanitizes keys so only alphavalues are allowed
					if(!is_int($key[$x]))
					{
						if(mysql_num_rows($query) > 1)
						$this->result[$i][$key[$x]] = $r[$key[$x]];
						else if(mysql_num_rows($query) < 1)
						$this->result = null;
						else
						$this->result[$i][$key[$x]] = $r[$key[$x]];
					}
				}
			}
			return true;
		}
		else
		{
			return false;
		}
	}

	/*
	 * Insert values into the table
	 * Required: table (the name of the table)
	 *           values (the values to be inserted)
	 * Optional: rows (if values don't match the number of rows)
	 */
	public function insert($query)
	{
		$ins = @mysql_query($query);

		if($ins)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/*
	 * Deletes table or records where condition is true
	 * Required: table (the name of the table)
	 * Optional: where (condition [column =  value])
	 */
	public function delete($query)
	{
		$del = @mysql_query($query);

		if($del)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/*
	 * Updates the database with the values sent
	 * Required: table (the name of the table to be updated
	 *           rows (the rows/values in a key/value array
	 *           where (the row/condition in an array (row,condition) )
	 */
	public function update($query)
	{
		$query = @mysql_query($query);
		if($query)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/*
	 * Returns the result set
	 */
	public function getResult()
	{
		return $this->result;
	}
}
?>